package com.platform.web.controller.report;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.module.domain.*;
import com.platform.module.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备统计报表控制类
 *
 * @author wangqi
 * @date 2020-08-04
 */
@Controller
@RequestMapping("/report/equipment")
public class EquipmentReportController extends BaseController {

    private String prefix = "report/equipment";

    @Autowired
    private IBaseValveTypeService baseValveTypeService;

    @Autowired
    private IBaseValveDataService baseValveDataService;

    @Autowired
    private IBaseCalorimeterTypeService baseCalorimeterTypeService;

    @Autowired
    private IBaseCalorimeterDataService baseCalorimeterDataService;

    @Autowired
    private IBaseConcentratorTypeService baseConcentratorTypeService;

    @Autowired
    private IBaseConcentratorDataService baseConcentratorDataService;

    @Autowired
    private RedisUtil redisUtil;

    @RequiresPermissions("report:equipment:view")
    @GetMapping()
    public String equipment()
    {
        return prefix + "/equipment";
    }

    /**
     * 阀门类型统计
     */
    @PostMapping("/valveTypeSummary")
    @ResponseBody
    public AjaxResult valveTypeSummary()
    {
        List<BaseValveType> valveTypeList  = baseValveTypeService.list();
        for(BaseValveType valveType : valveTypeList){
            int count = baseValveDataService.count(new QueryWrapper<BaseValveData>().eq("type_id", valveType.getTypeId()));
            valveType.setCount(count);
        }
        return AjaxResult.success(valveTypeList);
    }

    /**
     * 阀门数据统计
     */
    @PostMapping("/valveDataSummary")
    @ResponseBody
    public AjaxResult valveDataSummary()
    {
        List<BaseValveData> valveDataList = new ArrayList<BaseValveData>();

        BaseValveData valveData = null;

        /**
         * 查询未关联阀门
         */
        valveData = new BaseValveData();
        List<BaseValveData> unallocatedList = baseValveDataService.selectUnallocatedList(valveData);
        BaseValveData baseValveData = null;
        if(unallocatedList!=null && unallocatedList.size()>0) {
            baseValveData = new BaseValveData();
            baseValveData.setTypeName("未关联阀门");
            baseValveData.setCount(unallocatedList.size());
            valveDataList.add(baseValveData);
        }

        valveData = new BaseValveData();
        valveData.setStatus("0");
        List<BaseValveData> allocatedList1 = baseValveDataService.selectAllocatedList(valveData);
        if(allocatedList1!=null && allocatedList1.size()>0) {
            baseValveData = new BaseValveData();
            baseValveData.setTypeName("未登记阀门");
            baseValveData.setCount(allocatedList1.size());
            valveDataList.add(baseValveData);
        }

        valveData = new BaseValveData();
        valveData.setStatus("1");
        List<BaseValveData> allocatedList2 = baseValveDataService.selectAllocatedList(valveData);
        if(allocatedList2!=null && allocatedList2.size()>0) {
            baseValveData = new BaseValveData();
            baseValveData.setTypeName("已登记阀门");
            baseValveData.setCount(allocatedList2.size());
            valveDataList.add(baseValveData);
        }

        return AjaxResult.success(valveDataList);
    }

    /**
     * 热表类型统计
     */
    @PostMapping("/calorTypeSummary")
    @ResponseBody
    public AjaxResult calorTypeSummary()
    {
        List<BaseCalorimeterType> calorimeterTypeList  = baseCalorimeterTypeService.list();
        for(BaseCalorimeterType calorimeterType : calorimeterTypeList){
            int count = baseCalorimeterDataService.count(new QueryWrapper<BaseCalorimeterData>().eq("type_id", calorimeterType.getTypeId()));
            calorimeterType.setCount(count);
        }
        return AjaxResult.success(calorimeterTypeList);
    }

    /**
     * 热表数据统计
     */
    @PostMapping("/calorDataSummary")
    @ResponseBody
    public AjaxResult calorDataSummary()
    {
        List<BaseCalorimeterData> calorimeterDataList = new ArrayList<BaseCalorimeterData>();

        BaseCalorimeterData calorimeterData = null;

        /**
         * 查询未关联阀门
         */
        calorimeterData = new BaseCalorimeterData();
        List<BaseCalorimeterData> unallocatedList = baseCalorimeterDataService.selectUnallocatedList(calorimeterData);
        BaseCalorimeterData baseCalorimeterData = null;
        if(unallocatedList!=null && unallocatedList.size()>0) {
            baseCalorimeterData = new BaseCalorimeterData();
            baseCalorimeterData.setTypeName("未关联热表");
            baseCalorimeterData.setCount(unallocatedList.size());
            calorimeterDataList.add(baseCalorimeterData);
        }

        calorimeterData = new BaseCalorimeterData();
        calorimeterData.setStatus("0");
        List<BaseCalorimeterData> allocatedList1 = baseCalorimeterDataService.selectAllocatedList(calorimeterData);
        if(allocatedList1!=null && allocatedList1.size()>0) {
            baseCalorimeterData = new BaseCalorimeterData();
            baseCalorimeterData.setTypeName("未登记热表");
            baseCalorimeterData.setCount(allocatedList1.size());
            calorimeterDataList.add(baseCalorimeterData);
        }

        calorimeterData = new BaseCalorimeterData();
        calorimeterData.setStatus("1");
        List<BaseCalorimeterData> allocatedList2 = baseCalorimeterDataService.selectAllocatedList(calorimeterData);
        if(allocatedList2!=null && allocatedList2.size()>0) {
            baseCalorimeterData = new BaseCalorimeterData();
            baseCalorimeterData.setTypeName("已登记热表");
            baseCalorimeterData.setCount(allocatedList2.size());
            calorimeterDataList.add(baseCalorimeterData);
        }
        return AjaxResult.success(calorimeterDataList);
    }

    /**
     * 采集器类型统计
     */
    @PostMapping("/centraTypeSummary")
    @ResponseBody
    public AjaxResult centraTypeSummary()
    {
        List<BaseConcentratorType> concentratorTypeList  = baseConcentratorTypeService.list();
        for(BaseConcentratorType concentratorType : concentratorTypeList){
            int count = baseConcentratorDataService.count(new QueryWrapper<BaseConcentratorData>().eq("type_id", concentratorType.getTypeId()));
            concentratorType.setCount(count);
        }
        return AjaxResult.success(concentratorTypeList);
    }

    /**
     * 采集器类型统计
     */
    @PostMapping("/centraDataSummary")
    @ResponseBody
    public AjaxResult centraDataSummary()
    {
        List<BaseConcentratorData> concentratorDataList = new ArrayList<BaseConcentratorData>();

        int allocated = 0, unallocated = 0;

        List<BaseConcentratorData> list = baseConcentratorDataService.list();
        for(BaseConcentratorData baseConcentratorData : list){
            if(redisUtil.hHasKey("sys-netty", baseConcentratorData.getConcentratorId())){
                allocated++;
            }else{
                unallocated++;
            }
        }

        BaseConcentratorData baseConcentratorData = null;
        baseConcentratorData = new BaseConcentratorData();
        baseConcentratorData.setTypeName("在线采集器");
        baseConcentratorData.setCount(allocated);
        concentratorDataList.add(baseConcentratorData);

        baseConcentratorData = new BaseConcentratorData();
        baseConcentratorData.setTypeName("离线采集器");
        baseConcentratorData.setCount(unallocated);
        concentratorDataList.add(baseConcentratorData);

        return AjaxResult.success(concentratorDataList);
    }
}
